package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    //获取所有套餐信息
    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        try{
            //创建一个list集合
            List<Setmeal> list = null;
            //取出jedispool对象
            Jedis jedis = jedisPool.getResource();
            //设置jedis为(String Key,String value),设置键redisSetmealsKey，通过该键取出对应的值redisValue
            String redisValue = jedis.get("redisSetmealsKey");
            //对redisValue进行判断
            if (redisValue != null){
                //如果redisValue不为空，则说明redis中存有数据，通过JSON.parseArray(String text,Class(T) clazz)将redisValue转换为list
                list = JSON.parseArray(redisValue,Setmeal.class);
                System.out.println("1111111111");
                //将list提交到前端
                return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, list);
            }else {
                //如果redisValue为空，则说明reidis中无值，从DB中调用findAll方法获取数据
                list = setmealService.findAll();
                //将从DB中取出的值，调用jedis的setex(String Key, senconds, String Value)存到redis缓存中
                jedis.setex("redisSetmealsKey",60*10, JSON.toJSON(list).toString());
                System.out.println("2222222222");
                //将list返回前端
                return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, list);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    //根据id查询套餐信息
    @RequestMapping("/findById")
    public Result findById(int id){
        try{
            Setmeal setmeal1 = null;
            //获取连接池对象
            Jedis jedis = jedisPool.getResource();
            //redis(Strng key, String value),将套餐id设置为key，利用方法Integer.toString()将id转换为String类型，通过get方法获取value的值setmeal
            String setmeal = jedis.get(Integer.toString(id));
            //对setmeal进行判断
            if (setmeal != null){
                //如果setmeal不为空，说明有数据，调用JSON方法parseObject(String text, Class(T) clazz)将setmeal转换成Setmeal类型setmeal1
                setmeal1 = JSON.parseObject(setmeal, Setmeal.class);
                System.out.println("333333333333333");
                //将结果返回到前端
                return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal1);
            }else {
                //如果redis中无数据，则通过findById方法从DB取出数据setmeal1
                setmeal1 = setmealService.findById(id);
                //调用JSON方法JSON.toJSON().toString()将setmeal1转换成JSON字符串
                //调用jedis的方法setex(String Key, senconds, String Value)将setmeal1存到redis缓存中，key值用套餐id，
                String set = jedis.setex(Integer.toString(id),60*10,JSON.toJSON(setmeal1).toString());
                System.out.println("44444444444444");
                //将结果返回前端
                return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
